package com.hyt.it.ogt.pj.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.pj.feign.KwClient;
import com.hyt.it.ogt.pj.model.entity.ApiFail;
import com.hyt.it.ogt.pj.model.entity.Project;
import com.hyt.it.ogt.pj.service.IProjectService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理别的服务调用失败的任务
 *
 * @author liuq
 * @since 2021-08-02
 */
@Component
@Slf4j
public class ApiFailJob {

    @Autowired
    KwClient kwClient;

    @Autowired
    private IProjectService iProjectService;

    /**
     * 推送评卷任务
     */
    @XxlJob("pushMarkTaskJob")
    public void pushMarkTaskJob() throws Exception {
        log.info("执行推送评卷任务");
        ApiResponse<List<ApiFail>> apiResponse = kwClient.apiFailList("/markApi/pushMarkTask");
        if (apiResponse != null && apiResponse.getData() != null && !apiResponse.getData().isEmpty()) {
            List<ApiFail> apiFailList = apiResponse.getData();
            JSONArray idArray = new JSONArray();
            for (ApiFail apiFail : apiFailList) {
                JSONObject param = JSONObject.parseObject(apiFail.getParam());
                String projectId = (String) param.get("examId");
                String officeId = (String) param.get("officeId");
                log.info("开始推送评卷任务,officeId：{},projectId：{}", officeId, projectId);
                try {
                    Project project = iProjectService.getById(projectId);
                    if (project != null && project.getIsInit()) {
                        // 评卷项目已经初始化过，直接获取增量评卷数据
                        iProjectService.addProjectStudentForTask(projectId, officeId);
                    } else {
                        // 评卷项目未初始化，开始初始化评卷项目
                        iProjectService.initProjectForTask(projectId, officeId);
                    }
                    idArray.add(apiFail.getId());
                    log.info("评卷任务推送成功,officeId：{},projectId：{}", officeId, projectId);
                } catch (Exception e) {
                    log.error("评卷任务推送失败,officeId：{},projectId：{}", officeId, projectId, e);
                }
            }
            kwClient.apiFailDelete(idArray.toJSONString());
            XxlJobHelper.log("执行推送评卷任务成功");
        }
    }

    /**
     * 推送评卷任务
     */
    @XxlJob("pushMarkTaskJobExamId")
    public void pushMarkTaskJobExamId() throws Exception {
        log.info("执行推送评卷任务");
        String xxlparam = XxlJobHelper.getJobParam(); // 执行参数
        log.info("执行推送评卷任务xxlparam：{}", xxlparam);
        List<ApiFail> apiFailList = new ArrayList<ApiFail>();
        try {
            if (StringUtils.isNotEmpty(xxlparam)) {
                String[] group = xxlparam.split("\\|");
                if (null != group && group.length > 0) {
                    for (String officeExamStr : group) {
                        if (StringUtils.isNotEmpty(officeExamStr)) {
                            ApiFail apiFail = new ApiFail();
                            JSONObject param = JSONObject.parseObject(officeExamStr);
                            String projectId = (String) param.get("examId");
                            String officeId = (String) param.get("officeId");
                            apiFail.setOfficeId(officeId);
                            apiFail.setProjectId(projectId);
                            apiFailList.add(apiFail);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.info("error", e);
        }

        log.info("执行推送评卷任务apiFailList：{}", JSONObject.toJSON(apiFailList));

        JSONArray idArray = new JSONArray();
        for (ApiFail apiFail : apiFailList) {
            log.info("开始推送评卷任务,officeId：{},projectId：{}", apiFail.getOfficeId(), apiFail.getProjectId());
            try {
                Project project = iProjectService.getById(apiFail.getProjectId());
                if (project != null && project.getIsInit()) {
                    // 评卷项目已经初始化过，直接获取增量评卷数据
                    iProjectService.addProjectStudentForTask(apiFail.getProjectId(), apiFail.getOfficeId());
                } else {
                    // 评卷项目未初始化，开始初始化评卷项目
                    iProjectService.initProjectForTask(apiFail.getProjectId(), apiFail.getOfficeId());
                }
                idArray.add(apiFail.getId());
                log.info("评卷任务推送成功,officeId：{},projectId：{}", apiFail.getOfficeId(), apiFail.getProjectId());
            } catch (Exception e) {
                log.error("推送异常：", e);
                log.info("评卷任务推送失败,officeId：{},projectId：{}", apiFail.getOfficeId(), apiFail.getProjectId());
            }
        }
        kwClient.apiFailDelete(idArray.toJSONString());
        XxlJobHelper.log("执行推送评卷任务成功");
    }

}
